package yi.wuhenbot.Utils;

import org.apache.logging.log4j.util.Base64Util;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import yi.wuhenbot.Controller.BotController;
import cn.hutool.http.*;

/**
 * @author wuhen
 */
@Service
public class QQBotUtil {

    private static final Logger logger = LoggerFactory.getLogger(BotController.class);
    @Autowired
    private  BotTokenUtil botTokenUtil;
    /**
     * 获取yml的appid
     * @return appid
     */
    @Value("${bot.appid}")
    private String botAppId;
    /**
     * 发送私聊消息
     *
     * @param userOpenid 用户openid
     * @param content    消息内容
     */
    public void sendPrivateMessage(String userOpenid, String content, String contentId) {
        System.out.println("发送私聊消息");
        // 请求发送接口将数据将content返回
        String url ="https://api.sgroup.qq.com/v2/users/"+userOpenid+"/messages";
        // 构建body报文
        JSONObject body = new JSONObject();
        body.put("content", content);
        body.put("msg_type", 0);
        body.put("msg_id", contentId);

        // 构建header报文
        JSONObject headers = new JSONObject();
        headers.put("Authorization", "QQBot " + botTokenUtil.getToken());
        //logger.info("机器人Authorization："+ headers.getString("Authorization"));
        headers.put("Content-Type", "application/json");

        // 发送post请求
        String response = HttpUtils.sendPostRequest(url, headers, body.toString());
        //logger.info("请求url：{}",url);
        //logger.info("请求结果：{}", response);
    }
    /**
     * 发送群消息
     *
     * @param groupOpenid 群openid
     * @param content     消息内容
     */
    public void sendGroupMessage(String groupOpenid, String content, String contentId) {
        // 请求发送接口将数据将content返回
        String url ="https://api.sgroup.qq.com/v2/groups/"+groupOpenid+"/messages";
        // 构建body报文
        JSONObject body = new JSONObject();
        body.put("content", content);
        body.put("msg_type", 0);
        body.put("msg_id", contentId);

        // 构建header报文
        JSONObject headers = new JSONObject();
        headers.put("Authorization", "QQBot " + botTokenUtil.getToken());
        //logger.info("机器人Authorization："+ headers.getString("Authorization"));
        headers.put("Content-Type", "application/json");

        // 发送post请求
        String response = HttpUtils.sendPostRequest(url, headers, body.toString());
       // logger.info("请求url：{}",url);
        //logger.info("请求结果：{}", response);
    }
    /**
     * 发送频道消息
     *
     * @param channelId 频道id
     * @param content   消息内容
     * @param contentId 消息id
     */
    public void sendChannelGroupMessage(String channelId, String content, String contentId) {
        // 请求发送接口将数据将content返回
        String url ="https://api.sgroup.qq.com/channels/"+channelId+"/messages";
        logger.info("请求url：{}",url);
        // 构建body报文
        JSONObject body = new JSONObject();
        body.put("content", content);
        body.put("msg_type", 0);
        body.put("msg_id", contentId);

        // 构建header报文
        JSONObject headers = new JSONObject();
        headers.put("Authorization", "QQBot " + botTokenUtil.getToken());
        logger.info("机器人Authorization："+ headers.getString("Authorization"));
        headers.put("Content-Type", "application/json");

        // 发送post请求
        String response = HttpUtils.sendPostRequest(url, headers, body.toString());
        logger.info("请求url：{}",url);
        logger.info("请求结果：{}", response);
    }
    /**
     * 发送频道私信消息
     * @param guildId 频道私信用户id
     * @param content 消息内容
     * @param contentId 消息id

     */
    public  void sendChannelPrivateMessage(String guildId, String content,String contentId) {
        // 请求发送接口将数据将content返回
        String url ="https://api.sgroup.qq.com/dms/"+guildId+"/messages";
        // 构建body报文
        JSONObject body = new JSONObject();
        body.put("content", content);
        body.put("msg_type", 0);
        body.put("msg_id", contentId);

        // 构建header报文
        JSONObject headers = new JSONObject();
        headers.put("Authorization", "QQBot " + botTokenUtil.getToken());
        //logger.info("机器人Authorization："+ headers.getString("Authorization"));
        headers.put("Content-Type", "application/json");

        // 发送post请求
        String response = HttpUtils.sendPostRequest(url, headers, body.toString());
       // logger.info("请求url：{}",url);
        //logger.info("请求结果：{}", response);

    }
    // 获取用户头像
    /**
     * 获取用户头像
     * @param userOpenid 用户openid
     * @return 头像url
     */
    public String getUserAvatar(String userOpenid) {
        // 请求获取用户头像接口
        String URL= "https://q.qlogo.cn/qqapp/"+botAppId+"/"+userOpenid+"/640";

        // 构建header报文
        JSONObject headers = new JSONObject();
        headers.put("Content-Type", "application/json");
        logger.info("请求url：{}",URL);
        // 发送get请求
        String response = HttpUtil.get(URL);
        // 内容返回是图片，将图片转成base64编码
        String base64 = Base64Util.encode(response);

        logger.info("请求结果：{}", base64);
        return base64;


    }
}
